package edu.hawaii.wattwaiter.wattdepotcommands;

import java.util.ArrayList;
import java.util.List;
import org.wattdepot.client.WattDepotClient;

/**
 * Creates a client for interaction with a WattDepot server.  It does nothing but
 * establish a connection with the server.  It contains several variables to help establish
 * a standard for any commands that will interact with the server.
 * 
 * @author David Mau
 */
public class WattDepotClientCommand {
  /** To print debug messages to the console or not. */
  boolean consoleDebugMode = false;
  /** The default server for the commands. */
  String defaultServer = "http://server.wattdepot.org:8182/wattdepot/";
  /** The current client that has been created. */
  WattDepotClient client;
  
  /** The source where the data should be extracted from. */
  String oahuGrid = "SIM_OAHU_GRID";
  
  // Error list
  /** A List to return when an error occurs for methods that return ArrayLists of Doubles. */
  List<Double> allZeroList = new ArrayList<Double>();
  
  /** Server connection error message. */
  static String serverConnectionError = "Failed to connect to ";
  /** An error that occurs when a bad date is given. */
  static String badDate = "Bad date given: ";
  /** Not authorized to view source error message. */
  static String notAuthorized = "Not authorized to view source.\nError: ";
  /** Resource not found error message. */
  static String resourceNotFound = "Resource not found.\nError: ";
  /** Bad XML returned error message. */
  static String badXml = "Bad XML returned.\nError: ";
  /** Miscellaneous client error message. */
  static String miscClient = "Miscellaneous client error.\nError: ";
  /** Error for giving a null as a date argument. */
  static String nullDate = "No date passed to the program.";
  
  /** Error for giving null arguments to a command. */
  static String nullArguments = "One or more argruments given are null.";
  /** Error for not giving carbon or energy as an argument for which to collect data for. */
  static String notCarbonOrEnergy = "Choose either carbon or energy to collect data for.\n" +
      "Argument given: ";
  /** Error for not giving hour, day, or week as an argument for the frequency of data for the
   * output list. */
  static String notHourDayWeek = "Choose either hour, day, or week as a frequency to " +
      "collect data for.\nArgument given: ";
  /** Error for having a start date not being before than the end date. */
  static String temporalError = "Start date given is not before the end date.";
  /** Error for giving a null as the source to get sub-sources for. */
  static String nullSource = "No source passed to the program.";
  
  /**
   * Creates a client used for any WattWaiter client command.  Used mostly to define another
   * server to contact for information.  It also readies a List for returning
   * when an error occurs.
   * 
   * @param anotherServer An optional argument that can be given to attempt to
   * connect to a different WattDepot server than the default.
   */
  public WattDepotClientCommand( String anotherServer ) {
    // If another server is not provided,
    if ( anotherServer == null ) {
      // Use the default server.
      client = new WattDepotClient( defaultServer );
    }
    else {
      // Use the specified server.
      client = new WattDepotClient( anotherServer );
    }
    
    // Check to see if the client is healthy upon creation.
    if ( consoleDebugMode && !client.isHealthy() ) {
      System.out.println( serverConnectionError + client.getWattDepotUri() );
    }
    
    // Set up the error list for when things go very wrong.
    for ( int counter = 0; counter < 24; counter++ ) {
      allZeroList.add( 0.0 );
    }
  }
  
  /**
   * A method for turning on the console debug mode.
   */
  public void debugModeOn () {
    consoleDebugMode = true;
  }
}
